//
//  OrderViewController.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/4/18.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderTableViewCell.h"

@interface OrderViewController ()

{
    BOOL isOpen;
}

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    isOpen = YES;
    //设置返回按钮
    [self.backButton setImage:[UIImage imageNamed:@"back_white_btn"] forState:UIControlStateNormal];
    [self.backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
    
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.separatorStyle = NO;
    //设置header的两个button
    [self.orderButton setImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
    [self.orderButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.orderButton setTitle:@"立即排单" forState:UIControlStateNormal];
    [self.orderButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -150, 0, 0)];
    
    [self.shoppingButton setImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
    [self.shoppingButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.shoppingButton setTitle:@"购买排单币" forState:UIControlStateNormal];
    [self.shoppingButton setTitleEdgeInsets:UIEdgeInsetsMake(0,-150, 0, 0)];
        
}
- (IBAction)backButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)orderButton:(id)sender {
}
- (IBAction)shoppingButton:(id)sender {
}



#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.selectedIndex.row && self.selectedIndex != nil)
    {
        if (isOpen == YES)
        {
            return 150;
        }
        else
        {
            return 75;
        }
    }
    return 75;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTableViewCell *orderCell;
    if (indexPath.row == _selectedIndex.row && _selectedIndex != nil)
    {
        //如果是展开
        if (isOpen == YES)
        {
            static NSString *cellIdentifier = @"OrderCell";
            orderCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][0];
            if (!orderCell)
            {
                orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][0];
            }

            NSLog(@"new cell");
        }
        else
        {
            static NSString *cellIdentifier = @"orderCell2";
            orderCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][1];
            if (!orderCell)
            {
                orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][1];
            }
            
            NSLog(@"old cell");
        }
    }
    else
    {
        static NSString *cellIdentifier = @"orderCell2";
        orderCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][1];
        if (!orderCell)
        {
            orderCell = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil][1];
        }

    }

    return orderCell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    
    if (self.selectedIndex != nil && indexPath.row == _selectedIndex.row)
    {
        isOpen = !isOpen;
    }
    //判断选中不同row状态时候
    else if (self.selectedIndex != nil && indexPath.row != _selectedIndex.row)
    {
        //将选中的和所有索引都加进数组中
        indexPaths = [NSArray arrayWithObjects:indexPath,_selectedIndex, nil];
        isOpen = YES;
        
    }

    //记下选中的索引
    self.selectedIndex = indexPath;
    
    //刷新
    [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
